Attribute VB_Name = "mdlGestor"
Public Function Firmar(codPedido As Long, tablaEnc As String) As Boolean
    Dim cedulaBioanalista As String
    Dim conexion As New adodb.Connection
    Dim sql As String
    'codPedido = getPedidoSel
    cedulaBioanalista = MdlFunciones.cedBioanalista(MainModule.UsuarioEntro)
    If cedulaBioanalista = "0" Then Exit Function
    sql = "UPDATE [" & tablaEnc & "] SET cedula_bioanalista='" & cedulaBioanalista & "' WHERE cod_pedido=" & codPedido
    Database.conect conexion
    conexion.Execute sql
    sql = "UPDATE detalles SET st ='f' WHERE st='g' AND cod_pedido=" & codPedido
    conexion.Execute sql
    Database.closeDb conexion
End Function
Function existeDetalle(ByVal Detalle As Long, detalles() As Long, ByVal Total As Integer)
Dim existe As Boolean
Dim i As Integer
    existe = False
    For i = 0 To Total
        If detalles(i) = Detalle Then
            existe = True
            Exit For
        End If
    Next i
    existeDetalle = existe
End Function
Function ObtenerCodExamen(ByVal TablaDet As String, ByVal Detalle As String)
Dim sql As String
    Detalle = Val(Detalle)
    sql = "SELECT DETALLES.Cod_examen FROM [" & TablaDet & "] " _
    & " INNER JOIN DETALLES ON [" & TablaDet & "].Cod_Detalle = DETALLES.Cod_detalle " _
    & " WHERE DETALLES.Cod_Detalle=" & Detalle
    
    Dim t As DAO.Recordset
    Set t = Base.OpenRecordset(sql)
    cod_examen = 0
    If Not t.EOF Then
        cod_examen = t(0)
    End If
    ObtenerCodExamen = cod_examen
End Function
Function ObtenerCodPedido(ByVal TablaDet As String)
Dim sql As String
    sql = "SELECT DETALLES.Cod_Pedido FROM [" & TablaDet & "] " _
    & " INNER JOIN DETALLES ON [" & TablaDet & "].Cod_Detalle = DETALLES.Cod_detalle "
    Dim t As DAO.Recordset
    Set t = Base.OpenRecordset(sql)
    cod_pedido = 0
    If Not t.EOF Then
        cod_pedido = t(0)
    End If
    ObtenerCodPedido = cod_pedido
End Function
Sub Grabar(ByVal tablaEnc As String, ByVal TablaDet As String, ByVal CampoClave As String, ByVal formulario As Object)
    Dim t As DAO.Recordset
    Dim D As DAO.Recordset
    sta = "g"
    Set t = Base.OpenRecordset(tablaEnc)
    Set D = Base.OpenRecordset(UCase(TablaDet))
    t.Index = "pedido"
    t.Seek "=", formulario.txtPedido.Text
    If t.NoMatch Then
        t.AddNew
        e = AutoNumero(CampoClave, tablaEnc)
        t(0).value = e
    Else
        t.Edit
        e = t(0).value
        Base.Execute "DELETE FROM[" & UCase(TablaDet) & "] WHERE [" & CampoClave & "] =" & t(0)
    End If
    t(1).value = formulario.txtPedido.Text
    t(2).value = Date
    t(3).value = Time
    t.Update
    For i = 0 To formulario.List1.ListCount - 1
        If Not IsNumeric(formulario.List4.list(i)) Then GoTo nexti
        D.AddNew
            D(0).value = e
            D(1).value = formulario.List4.list(i)
            D(2).value = formulario.List2.list(i)
            D(3).value = formulario.List5.list(i)
            D("cod_relacion").value = Val(formulario.List3.list(i))
            Base.Execute "UPDATE detalles SET st='" & sta & "'WHERE cod_detalle=" & formulario.List4.list(i)
        D.Update
        Dim rsV  As DAO.Recordset
        cod_detalle = formulario.List4.list(i)
        cod_examen = ObtenerCodExamen(TablaDet, cod_detalle)
        cod_relacion = Val(formulario.List3.list(i))
        If Val(cod_relacion) = 0 Then
            Set rsV = Base.OpenRecordset("SELECT *FROM valores WHERE cod_examen=" & cod_examen)
            cod = cod_examen
            Base.Execute "DELETE FROM imp_valores WHERE cod_detalle=" & cod_detalle
        Else
            Set rsV = Base.OpenRecordset("SELECT *FROM valores WHERE cod_examen=" & cod_relacion)
            cod = cod_relacion
        End If
        cod_orden = 0
        Base.Execute "DELETE FROM imp_valores WHERE cod_detalle=" & cod_detalle & " AND cod_examen=" & cod
        While Not rsV.EOF
            cod_orden = cod_orden + 1
            Base.Execute "INSERT INTO imp_valores(cod_detalle,valor,cod_orden,cod_examen) VALUES(" & cod_detalle & ",'" & rsV("valor").value & "'," & cod_orden & "," & cod_relacion & ")"
            rsV.MoveNext
         Wend
nexti:
    Next i
End Sub

Sub mostrarDetalle(Encabezado, Detalle, consulta, grupo, consultaP As String, formulario As Form)
    Dim tEncabezado As DAO.Recordset
    Dim D As DAO.Recordset
    Dim sqlQuery As String
    Dim isSettingPerfil As Boolean
    Dim detalles(50) As Long
    Dim contDetalles As Integer
    contDetalles = -1
    Set tEncabezado = Base.OpenRecordset(Encabezado)
    Set D = Base.OpenRecordset(UCase(Detalle))
    formulario.List1.Clear
    formulario.List2.Clear
    formulario.List3.Clear
   
    tEncabezado.Index = "pedido"
    tEncabezado.Seek "=", formulario.txtPedido.Text
    
    If Not tEncabezado.EOF Then
        sqlQuery = "SELECT * FROM " & UCase(consulta) & " WHERE cod_pedido=" & formulario.Pedido _
            & " AND idGrupo=" & grupo & " AND st<>'p'"
        Set tEncabezado = Base.OpenRecordset(sqlQuery)
        isSettingPerfil = False
        While Not tEncabezado.EOF
           If Not EsPerfilRes(tEncabezado("COD_EXAMEN")) Then
                formulario.List1.AddItem tEncabezado("descripcion")
                formulario.List3.AddItem " "
                formulario.List4.AddItem tEncabezado("cod_detalle")
                formulario.List5.AddItem getObservacion(tEncabezado("observacion"))
                formulario.List2.AddItem tEncabezado("resultado")
                isSettingPerfil = False
            Else
                If Not existeDetalle(Val(tEncabezado("cod_detalle")), detalles, contDetalles) Then
                    Call cargarPerfil(formulario, tEncabezado("cod_examen"), tEncabezado("cod_pedido"), consultaP)
                End If
            End If
            contDetalles = contDetalles + 1
            detalles(contDetalles) = Val(tEncabezado("cod_detalle"))
            tEncabezado.MoveNext
        Wend
    End If
    
    Set t = Base.OpenRecordset("select *from [Quimica FALTAN] where cod_pedido=" & formulario.Pedido & " AND idGrupo=" & grupo & " AND st='p'")
    While Not t.EOF
        If Not EsPerfilRes(t("COD_EXAMEN")) Then
            formulario.List1.AddItem t("descripcion")
            formulario.List3.AddItem 0
            formulario.List4.AddItem t("cod_detalle")
            FrmElegir.Label1.Caption = t("descripcion")
            FrmElegir.Show vbModal
            formulario.List5.AddItem FrmElegir.ob
            formulario.List2.AddItem FrmElegir.res
        Else
            Dim made As DAO.Recordset
            Set made = Base.OpenRecordset("select *from PerfileMade where cod_examen=" & t("COD_EXAMEN"))
            On Error Resume Next
            While Not made.EOF
                formulario.List1.AddItem made("EXAMENES_1.descripcion")
                formulario.List3.AddItem made("cod_examenr")
                formulario.List4.AddItem t("cod_detalle")
                FrmElegir.Label1.Caption = made("EXAMENES_1.descripcion")
                FrmElegir.Show vbModal
                formulario.List5.AddItem FrmElegir.ob
                formulario.List2.AddItem FrmElegir.res
                made.MoveNext
            Wend
        End If
        t.MoveNext
    Wend
End Sub

Function getObservacion(observacion) As String
    If IsNull(observacion) Then
        getObservacion = " "
    Else
        getObservacion = observacion
    End If
End Function

Private Sub cargarPerfil(formulario As Form, codExamen As Long, codPedido As Long, consultaP As String)
Dim sqlQuery As String
Dim tPerfil As DAO.Recordset
    sqlQuery = "SELECT * FROM perfilemade  WHERE cod_examen=" & codExamen
    Set tPerfil = Base.OpenRecordset(sqlQuery)
    While Not tPerfil.EOF
        resultadoPerfil = MdlFunciones.BuscarResultadoQuimica(codPedido, _
                                            tPerfil("EXAMENES_1.descripcion"), consultaP)
        obsePerfil = MdlFunciones.BuscarObservacionQuimica(codPedido, _
                                            tPerfil("EXAMENES_1.descripcion"), consultaP)
        detallePerfil = MdlFunciones.BuscarCodDetalleQuimica(codPedido, _
                                            tPerfil("EXAMENES_1.descripcion"), consultaP)
        formulario.List1.AddItem tPerfil("EXAMENES_1.descripcion")
        formulario.List3.AddItem tPerfil("cod_examenr")
        formulario.List4.AddItem detallePerfil
        formulario.List5.AddItem obsePerfil
        formulario.List2.AddItem resultadoPerfil
        tPerfil.MoveNext
    Wend
    
End Sub
Public Function CualForm(n)
On Error Resume Next
Dim t  As DAO.Recordset
Set t = Base.OpenRecordset("select idGrupo from examenes where cod_examen=" & n)
    If Not t.EOF Then
        If t(0).value = 1 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Hematologicos Enc", "Hematologicos Det", "Hematologicos", 1, "PHematologiA", "cod_Hematologicos", "Hematologicos"
        End If
        If t(0).value = 2 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Quimica Enc", "Quimica Det", "Quimica", 2, "PQuimica", "cod_quimica", "Quimica Sanguineas"
        End If
        If t(0).value = 3 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Serologicos Enc", "Serologicos Det", "Serologicos", 3, "PSerologiA", "cod_Serologicos", "Serologicos"
        End If
        If t(0).value = 4 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Inmunologia Enc", "Inmunologia Det", "Inmunologia", 4, "PInmunologia", "cod_Inmunologia", "Inmunologia"
        End If
        If t(0).value = 5 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "cHeces Enc", "cHeces Det", "cHeces", 5, "PHeces", "cod_heces", "Heces"
        End If
        If t(0).value = 7 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Bacteriologias Enc", "Bacteriologias Det", "Bact", 7, "PBacteriologias", "cod_Bacteriologia", "Bacteriologias"
        End If
        If t(0).value = 8 Then
            Set CualForm = frmOrina  'CERRADO
        End If
        If t(0).value = 9 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Urinaria Enc", "Urinaria Det", "Urinaria", 9, "PUrinarias", "cod_Urinaria", "Quimica Urinaria"
        End If
        If t(0).value = 10 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Hormonas Enc", "Hormonas Det", "Hormonas", 10, "PHormonas", "cod_Hormonas", "Hormonas"
        End If
        If t(0).value = 11 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Hepaticos Enc", "Hepaticos Det", "Hepaticos", 11, "PHepaticos", "cod_Hepaticos", "Marcadores Hepaticos"
        End If
        
        If t(0).value = 12 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Marcadores Enc", "Marcadores Det", "Marcadores", 12, "PMarcadores", "cod_Marcadores", "Marcadores Tumorales"
        End If
        If t(0).value = 15 Then
            Set CualForm = FrmAntibiograma 'CERRADO
        End If
        If t(0).value = 23 Then
            Set CualForm = FrmAntibiograma 'CERRADO
        End If
        If t(0).value = 26 Then
            Set CualForm = frmBiopsia 'CERRADO
        End If
        If t(0).value = 27 Then
            Set CualForm = frmCitologia 'CERRADO
        End If
        If t(0).value = 28 Then
            Set CualForm = frmSeriado 'CERRADO
        End If
        
        If t(0).value = 16 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Hepatitis Enc", "Hepatitis Det", "Hepatitis", 16, "PHepatitis", "cod_Hepatitis", "Hepatitis"
        End If
        
        If t(0).value = 17 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Antigenos Enc", "Antigenos Det", "Antigenos", 17, "PAntigenos", "cod_Antigenos", "Antigenos"
        End If
        If t(0).value = 18 Then
            Set CualForm = FrmQuimica
            CualForm.cargarValores "Otros Enc", "Otros Det", "Otros", 18, "POtros", "cod_Otros", "Pruebas Especiales"
        End If
         
        If t(0).value = 19 Then
            Set CualForm = frmHeces
            Exit Function
        End If
        If t(0).value = 20 Then
            Set CualForm = FrmAzucares
            Exit Function
        End If
        If t(0).value = 21 Then
            Set CualForm = FrmHematologias
            Exit Function
        End If
        If t(0).value = 22 Then
            Set CualForm = FrmFrotis
            Exit Function
        End If
    End If
End Function
